using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;

namespace FlexLib
{
	public class SocketWriter
	{
		private Socket _socket;

		public SocketWriter(Socket socket)
		{
			_socket = socket;
		}

		public void WriteBytes(byte[] bytes)
		{
			_socket.Send(bytes);
		}

		public void WriteBytes(byte[] bytes, int count)
		{
			_socket.Send(bytes, count, SocketFlags.None);
		}

		public void WriteInt(int number)
		{
			byte[] buffer = new byte[4];
			MemoryStream ms = new MemoryStream(buffer);
			BinaryWriter writer = new BinaryWriter(ms);
			writer.Write(number);
			writer.Close();

			WriteBytes(buffer);
		}

		public void WriteString(string str)
		{
			byte[] buffer = Encoding.UTF8.GetBytes(str);
			WriteInt(buffer.Length);
			WriteBytes(buffer);
		}

		public void Close()
		{
			_socket.Shutdown(SocketShutdown.Both);
			_socket.Close();
		}
	}
}
